package com.dataseek.iot.core.handler;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.response.ResponseEntity;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author cc
 * @Title: serviceHandler 抽象类
 * @Description: serviceHandler 抽象类
 * @date 2021/9/712:08 下午
 * @since 1.0
 */
public abstract class ServiceHandler<T> implements IServiceHandler {

    private ResponseEntity responseEntity;

    /**
     * 创建返回结果
     * @param b
     * @return
     */
    public Mono<ResponseEntity> createResponse(Boolean b){
        responseEntity = new ResponseEntity();
        if(b){
            responseEntity.setCode(ResponseConst.SUCCESS_CODE);
            responseEntity.setMsg(ResponseConst.SUCCESS);
        }else{
            responseEntity.setCode(ResponseConst.CRUD_FAILED_CODE);
            responseEntity.setMsg(ResponseConst.CRUD_FAILED);
        }
        responseEntity.setData(b);
        return Mono.just(responseEntity);
    };

    /**
     * 创建返回结果
     * @param entity
     * @return
     */
    public Mono<ResponseEntity> createResponse(T entity){
        responseEntity = new ResponseEntity();
        if(entity!=null){
            responseEntity.setCode(ResponseConst.SUCCESS_CODE);
            responseEntity.setMsg(ResponseConst.SUCCESS);
        }else{
            responseEntity.setCode(ResponseConst.CRUD_FAILED_CODE);
            responseEntity.setMsg(ResponseConst.CRUD_FAILED);
        }
        responseEntity.setData(entity);
        return Mono.just(responseEntity);
    };

    /**
     * 创建返回结果
     * @param p
     * @return
     */
    public Mono<ResponseEntity> createResponse(Page p){
        responseEntity = new ResponseEntity();
        if(p!=null){
            responseEntity.setCode(ResponseConst.SUCCESS_CODE);
            responseEntity.setMsg(ResponseConst.SUCCESS);
        }else{
            responseEntity.setCode(ResponseConst.CRUD_FAILED_CODE);
            responseEntity.setMsg(ResponseConst.CRUD_FAILED);
        }
        responseEntity.setData(p);
        return Mono.just(responseEntity);
    };

    /**
     * 创建返回结果
     * @param entity
     * @return
     */
    public Flux<ResponseEntity> createResponse(List<T> entity){
        responseEntity = new ResponseEntity();
        if(entity!=null){
            responseEntity.setCode(ResponseConst.SUCCESS_CODE);
            responseEntity.setMsg(ResponseConst.SUCCESS);
        }else {
            responseEntity.setCode(ResponseConst.CRUD_FAILED_CODE);
            responseEntity.setMsg(ResponseConst.CRUD_FAILED);
        }
        responseEntity.setData(entity);
        return Flux.just(responseEntity);
    };

}
